home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / ripley / source / idct.asm < prev    next >
Assembly Source File  |  1999-03-02  |  12KB  |  735 lines

  1.  
  2. ;********************************************************************
  3. ;
  4. ;    idct.asm
  5. ;
  6. ;    1.0 - 28.7.98 (bifat)    written in assembler
  7. ;    1.1 - 29.7.98 (bifat)    optimized for superscalarity,
  8. ;                inlined idctrow/idctcol into
  9. ;                the FastIDCT main loop
  10. ;    1.2 - 30.7.98 (bifat)    idctrow and idctcol heavily
  11. ;                optimized for superscalar execution
  12. ;    1.3 - 2.8.98 (bifat)    removed clipping. damn, i see no
  13. ;                fucking difference!
  14. ;    1.4 - 1.10.98 (bifat)   idctcol shortcut bug fixed
  15. ;                idctrow shortcut was not detected - fixed
  16. ;
  17. ;********************************************************************
  18.  
  19. W1    EQU    2841
  20. W2    EQU    2676
  21. W3    EQU    2408
  22. W5    EQU    1609
  23. W6    EQU    1108
  24. W7    EQU    565
  25.  
  26. ;********************************************************************
  27.  
  28.         section text
  29.  
  30.         XDEF    _Initialize_Fast_IDCT
  31.         XDEF    _Fast_IDCT
  32.  
  33. ;********************************************************************
  34.  
  35. iclip        ds.w    512
  36. iclp        ds.w    512
  37.  
  38. ;********************************************************************
  39.  
  40. _Initialize_Fast_IDCT:
  41.  
  42.         movem.l    a0/d0/d1,-(a7)
  43.  
  44.         lea    iclip(pc),a0
  45.  
  46.         move.w    #-256,d1
  47.         move.w    #256-1,d0
  48. .inilop        move.w    d1,(a0)+
  49.         dbf    d0,.inilop
  50.         
  51.         move.w    #-255,d1
  52.         move.w    #512-1,d0
  53. .inilop2    move.w    d1,(a0)+
  54.         addq.w    #1,d1
  55.         dbf    d0,.inilop2
  56.         
  57.         move.w    #255,d1
  58.         move.w    #256-1,d0
  59. .inilop3    move.w    d1,(a0)+
  60.         dbf    d0,.inilop3
  61.  
  62.         movem.l    (a7)+,a0/d0/d1
  63.         rts
  64.  
  65.  
  66. ;********************************************************************
  67.  
  68. idctrow:    MACRO
  69.  
  70.         move.l    d0,a6            ; *blk
  71.  
  72.         lea    2(a6),a5
  73.  
  74.         move.w    (a5)+,d1
  75.  
  76.         moveq    #11,d7
  77.  
  78.         move.w    (a5)+,d2
  79.  
  80.         move.w    d1,d0
  81.  
  82.         move.w    (a5)+,d3
  83.  
  84.         or.w    d2,d0
  85.  
  86.         move.w    (a5)+,d4
  87.  
  88.         or.w    d3,d0
  89.  
  90.         move.w    (a5)+,d5
  91.  
  92.         or.w    d4,d0
  93.  
  94.         move.w    (a5)+,d6
  95.  
  96.         ext.l    d4
  97.  
  98.         or.w    d5,d0
  99.  
  100.         asl.l    d7,d4
  101.  
  102.         move.w    (a5)+,d7
  103.  
  104.         or.w    d6,d0
  105.  
  106.         move.w    d7,a5
  107.         or.w    d7,d0
  108.  
  109.         bne.b    .cont1\@
  110.  
  111.  
  112.         move.w    (a6),d0
  113.         asl.w    #3,d0
  114.         move.w    d0,(a6)+
  115.         move.w    d0,(a6)+
  116.         move.w    d0,(a6)+
  117.         move.w    d0,(a6)+
  118.         move.w    d0,(a6)+
  119.         move.w    d0,(a6)+
  120.         move.w    d0,(a6)+
  121.         move.w    d0,(a6)+
  122.         bra    .ok\@
  123.         
  124.  
  125.         cnop    0,4
  126.  
  127. .cont1\@    moveq    #11,d7
  128.  
  129.         move.w    (a6),d0
  130.         ext.l    d0
  131.         asl.l    d7,d0
  132.         moveq    #-127,d7
  133.     
  134.         sub.l    d7,d0
  135.         move.l    d0,a0
  136.  
  137.         move.w    d1,d0
  138.         add.w    a5,d0
  139.         
  140.         muls.w    #W1-W7,d1
  141.         exg    a5,d7
  142.         muls.w    #W7,d0
  143.         
  144.         muls.w    #-(W1+W7),d7
  145.         exg    a5,d7
  146.         add.l    d0,d1
  147.  
  148.         add.l    d0,a5
  149.  
  150.         move.l    d5,d0
  151.         add.l    d3,d0
  152.         muls.w    #W3,d0
  153.             
  154.         muls.w    #-(W3-W5),d5
  155.         add.l    d0,d5
  156.  
  157.         muls.w    #-(W3+W5),d3
  158.         add.l    d0,d3
  159.  
  160.         move.l    a0,d0
  161.         add.l    d4,d0
  162.         move.l    d0,a1
  163.  
  164.         sub.l    d4,a0
  165.  
  166.         move.l    d2,d4
  167.         add.l    d6,d4
  168.         muls.w    #W6,d4
  169.  
  170.         muls.w    #-(W2+W6),d6
  171.         add.l    d4,d6
  172.  
  173.         muls.w    #(W2-W6),d2
  174.         add.l    d4,d2
  175.         
  176.         move.l    d1,d4
  177.         add.l    d5,d4
  178.  
  179.         sub.l    d5,d1
  180.  
  181.         move.l    a5,d5
  182.         add.l    d3,d5
  183.  
  184.         sub.l    d3,a5
  185.  
  186.         move.l    a1,d3
  187.         add.l    d2,d3
  188.  
  189.         sub.l    d2,a1
  190.  
  191.         move.l    a0,d2
  192.         add.l    d6,d2
  193.  
  194.         sub.l    d6,a0
  195.  
  196.         move.w    #-128,a2
  197.         move.l    #181,d7
  198.         
  199.         move.l    d1,d6
  200.         add.l    a5,d6
  201.         muls.l    d7,d6
  202.         sub.l    a5,d1
  203.         sub.l    a2,d6
  204.  
  205.         muls.l    d7,d1
  206.         asr.l    #8,d6
  207.         sub.l    a2,d1
  208.  
  209.         move.l    d3,d0
  210.         asr.l    #8,d1
  211.         move.l    d2,d7
  212.         add.l    d4,d0
  213.         add.l    d6,d7
  214.         asr.l    #8,d0
  215.         asr.l    #8,d7
  216.         move.w    d0,(a6)+
  217.         
  218.         move.l    a0,d0
  219.         move.w    d7,(a6)+
  220.         add.l    d1,d0
  221.         move.l    a1,d7
  222.         asr.l    #8,d0
  223.         add.l    d5,d7
  224.         move.w    d0,(a6)+
  225.         asr.l    #8,d7
  226.         move.l    a1,d0
  227.         move.w    d7,(a6)+
  228.     
  229.         sub.l    d6,d2
  230.         sub.l    d5,d0
  231.         asr.l    #8,d2
  232.         asr.l    #8,d0
  233.         move.w    d0,(a6)+
  234.  
  235.         move.l    a0,d0
  236.         sub.l    d4,d3
  237.         sub.l    d1,d0
  238.         asr.l    #8,d3
  239.         asr.l    #8,d0
  240.  
  241.         movem.w    d0/d2/d3,(a6)
  242.  
  243. .ok\@                
  244.         ENDM
  245.  
  246.  
  247. ;********************************************************************
  248.  
  249. idctcol:    MACRO
  250.  
  251.         move.l    d0,a6            ; *blk
  252.  
  253.         move.w    8*4*2(a6),d1
  254.  
  255.         lea    iclp(pc),a2
  256.  
  257.         move.w    8*6*2(a6),d2        ; x2
  258.  
  259.         move.l    d1,d0
  260.  
  261.         move.w    8*2*2(a6),d3        ; x3
  262.  
  263.         or.w    d2,d0
  264.  
  265.         move.w    8*1*2(a6),d4        ; x4
  266.  
  267.         or.w    d3,d0
  268.  
  269.         move.w    8*7*2(a6),d5        ; x5
  270.  
  271.         or.w    d4,d0
  272.  
  273.         move.w    8*5*2(a6),d6        ; x6
  274.  
  275.         or.w    d5,d0
  276.  
  277.         move.w    8*3*2(a6),d7        ; x7
  278.  
  279.         or.w    d6,d0
  280.  
  281.         move.w    d7,a5
  282.         or.w    d7,d0
  283.         bne.b    .cont1\@
  284.  
  285.         moveq    #32,d0
  286.         add.w    8*0*2(a6),d0
  287.         asr.w    #6,d0
  288.         move.w    (a2,d0.w*2),d0
  289.         
  290.         move.w    d0,8*0*2(a6)
  291.         move.w    d0,8*1*2(a6)
  292.         move.w    d0,8*2*2(a6)
  293.         move.w    d0,8*3*2(a6)
  294.         move.w    d0,8*4*2(a6)
  295.         move.w    d0,8*5*2(a6)
  296.         move.w    d0,8*6*2(a6)
  297.         move.w    d0,8*7*2(a6)
  298.         bra    .ok\@
  299.  
  300.         cnop    0,4
  301.  
  302. .cont1\@
  303.         ext.l    d1
  304.         asl.l    #8,d1            ; x1
  305.  
  306.  
  307.         move.w    8*0*2(a6),d7
  308.  
  309.         move.l    d4,d0
  310.  
  311.         ext.l    d7
  312.  
  313.         add.l    d5,d0
  314.  
  315.         asl.l    #8,d7
  316.  
  317.         muls.w    #W7,d0
  318.  
  319.         add.l    #8192,d7
  320.  
  321.         addq.l    #4,d0
  322.  
  323.         move.l    d7,a0
  324.  
  325.         muls.w    #W1-W7,d4
  326.  
  327.         muls.w    #-(W1+W7),d5
  328.  
  329.         add.l    d0,d4
  330.         add.l    d0,d5
  331.  
  332.         asr.l    #3,d4
  333.  
  334.         move.l    a5,d0
  335.         add.l    d6,d0
  336.         muls.w    #W3,d0
  337.  
  338.         asr.l    #3,d5
  339.  
  340.         addq.l    #4,d0
  341.  
  342.         muls.w    #-(W3-W5),d6
  343.  
  344.         exg    a5,d7
  345.  
  346.         add.l    d0,d6
  347.  
  348.         muls.w    #-(W3+W5),d7
  349.  
  350.         asr.l    #3,d6
  351.  
  352.         add.l    d0,d7
  353.         asr.l    #3,d7
  354.  
  355.         move.l    a0,d0
  356.  
  357.         exg    a5,d7
  358.  
  359.         add.l    d1,d0
  360.         sub.l    d1,a0
  361.         move.l    d2,d1
  362.         add.l    d3,d1
  363.         muls.w    #W6,d1
  364.  
  365.         muls.w    #-(W2+W6),d2
  366.  
  367.         addq.l    #4,d1
  368.  
  369.         add.l    d1,d2
  370.  
  371.         muls.w    #W2-W6,d3
  372.  
  373.         asr.l    #3,d2
  374.  
  375.         add.l    d1,d3
  376.         asr.l    #3,d3
  377.     
  378.         move.l    d6,d1
  379.         add.l    d4,d1
  380.         sub.l    d6,d4
  381.         move.l    a5,d6
  382.         add.l    d5,d6
  383.         sub.l    a5,d5
  384.         move.l    d0,a5
  385.         add.l    d3,a5
  386.         sub.l    d3,d0
  387.         move.l    d2,d3
  388.         add.l    a0,d3
  389.         sub.l    d2,a0
  390.  
  391.         move.l    d0,a1
  392.  
  393.         move.l    d4,d2
  394.  
  395.         moveq    #-128,d7
  396.  
  397.         add.l    d5,d2
  398.  
  399.         move.l    #181,d0
  400.         
  401.         sub.l    d5,d4
  402.  
  403.         muls.l    d0,d2
  404.         muls.l    d0,d4
  405.  
  406.         sub.l    d7,d2
  407.         sub.l    d7,d4
  408.  
  409.         asr.l    #8,d2
  410.  
  411.         move.l    a5,d7
  412.  
  413.         asr.l    #8,d4
  414.  
  415.         add.l    d1,d7
  416.  
  417.         moveq    #14,d5
  418.  
  419.         move.l    a5,d0
  420.  
  421.         asr.l    d5,d7
  422.  
  423.         sub.l    d1,d0
  424.  
  425.         move.w    d7,8*0*2(a6)
  426.  
  427.         asr.l    d5,d0
  428.  
  429.         move.l    d2,d7
  430.  
  431.         move.w    d0,8*7*2(a6)
  432.  
  433.         add.l    d3,d7
  434.  
  435.         move.l    a0,d0
  436.  
  437.         asr.l    d5,d7
  438.  
  439.         add.l    d4,d0
  440.  
  441.         move.w    d7,8*1*2(a6)
  442.  
  443.         asr.l    d5,d0
  444.  
  445.         move.l    a1,d7
  446.  
  447.         move.w    d0,8*2*2(a6)
  448.  
  449.         add.l    d6,d7
  450.  
  451.         move.l    a1,d0
  452.  
  453.         asr.l    d5,d7
  454.  
  455.         sub.l    d6,d0
  456.  
  457.         move.w    d7,8*3*2(a6)
  458.  
  459.         asr.l    d5,d0
  460.  
  461.         move.l    a0,d7
  462.  
  463.         move.w    d0,8*4*2(a6)
  464.  
  465.         sub.l    d4,d7
  466.  
  467.         sub.l    d2,d3
  468.  
  469.         asr.l    d5,d7
  470.         move.w    d7,8*5*2(a6)
  471.  
  472.         asr.l    d5,d3
  473.         move.w    d3,8*6*2(a6)
  474.  
  475. .ok\@
  476.         ENDM
  477.  
  478.  
  479. ;********************************************************************
  480.  
  481. _Fast_IDCT:
  482.         movem.l    d0-d7/a0-a6,-(a7)
  483.  
  484.         move.l    d0,a4
  485.  
  486.  
  487.         sub.l    a3,a3
  488. .rowlop
  489.         move.l    a3,d0
  490.         asl.w    #4,d0
  491.         add.l    a4,d0
  492.  
  493.         idctrow
  494.  
  495.         addq.w    #1,a3
  496.         cmp.w    #8,a3
  497.         bne    .rowlop
  498.  
  499.  
  500.         sub.l    a3,a3
  501. .collop
  502.         move.l    a3,d0
  503.         add.w    d0,d0
  504.         add.l    a4,d0
  505.  
  506.         idctcol
  507.  
  508.         addq.w    #1,a3
  509.         cmp.w    #8,a3
  510.         bne    .collop
  511.  
  512.  
  513. skip
  514.         movem.l    (a7)+,d0-d7/a0-a6
  515.         rts
  516.  
  517.  
  518.     END    
  519.         
  520.  
  521.  
  522.  
  523.  
  524. /* idct.c, inverse fast discrete cosine transform                           */
  525.  
  526. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  527.  
  528. /*
  529.  * Disclaimer of Warranty
  530.  *
  531.  * These software programs are available to the user without any license fee or
  532.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  533.  * any and all warranties, whether express, implied, or statuary, including any
  534.  * implied warranties or merchantability or of fitness for a particular
  535.  * purpose.  In no event shall the copyright-holder be liable for any
  536.  * incidental, punitive, or consequential damages of any kind whatsoever
  537.  * arising from the use of these programs.
  538.  *
  539.  * This disclaimer of warranty extends to the user of these programs and user's
  540.  * customers, employees, agents, transferees, successors, and assigns.
  541.  *
  542.  * The MPEG Software Simulation Group does not represent or warrant that the
  543.  * programs furnished hereunder are free of infringement of any third-party
  544.  * patents.
  545.  *
  546.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  547.  * are subject to royalty fees to patent holders.  Many of these patents are
  548.  * general enough such that they are unavoidable regardless of implementation
  549.  * design.
  550.  *
  551.  */
  552.  
  553. /**********************************************************/
  554. /* inverse two dimensional DCT, Chen-Wang algorithm       */
  555. /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984)             */
  556. /* 32-bit integer arithmetic (8 bit coefficients)         */
  557. /* 11 mults, 29 adds per DCT                              */
  558. /*                                      sE, 18.8.91       */
  559. /**********************************************************/
  560. /* coefficients extended to 12 bit for IEEE1180-1990      */
  561. /* compliance                           sE,  2.1.94       */
  562. /**********************************************************/
  563.  
  564. /* this code assumes >> to be a two's-complement arithmetic */
  565. /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2               */
  566.  
  567. #include "config.h"
  568.  
  569. #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
  570. #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
  571. #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
  572. #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
  573. #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
  574. #define W7 565  /* 2048*sqrt(2)*cos(7*pi/16) */
  575.  
  576. /* global declarations */
  577. void Initialize_Fast_IDCT _ANSI_ARGS_((void));
  578. void Fast_IDCT _ANSI_ARGS_((short *block));
  579.  
  580. /* private data */
  581. static short iclip[1024]; /* clipping table */
  582. static short *iclp;
  583.  
  584. /* private prototypes */
  585. static void idctrow _ANSI_ARGS_((short *blk));
  586. static void idctcol _ANSI_ARGS_((short *blk));
  587.  
  588. /* row (horizontal) IDCT
  589.  *
  590.  *           7                       pi         1
  591.  * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
  592.  *          l=0                      8          2
  593.  *
  594.  * where: c[0]    = 128
  595.  *        c[1..7] = 128*sqrt(2)
  596.  */
  597.  
  598. static void idctrow(blk)
  599. short *blk;
  600. {
  601.   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  602.  
  603.   /* shortcut */
  604.   if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
  605.         (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
  606.   {
  607.     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
  608.     return;
  609.   }
  610.  
  611.   x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
  612.  
  613.   /* first stage */
  614.   x8 = W7*(x4+x5);
  615.   x4 = x8 + (W1-W7)*x4;
  616.   x5 = x8 - (W1+W7)*x5;
  617.   x8 = W3*(x6+x7);
  618.   x6 = x8 - (W3-W5)*x6;
  619.   x7 = x8 - (W3+W5)*x7;
  620.   
  621.   /* second stage */
  622.   x8 = x0 + x1;
  623.   x0 -= x1;
  624.   x1 = W6*(x3+x2);
  625.   x2 = x1 - (W2+W6)*x2;
  626.   x3 = x1 + (W2-W6)*x3;
  627.   x1 = x4 + x6;
  628.   x4 -= x6;
  629.   x6 = x5 + x7;
  630.   x5 -= x7;
  631.   
  632.   /* third stage */
  633.   x7 = x8 + x3;
  634.   x8 -= x3;
  635.   x3 = x0 + x2;
  636.   x0 -= x2;
  637.   x2 = (181*(x4+x5)+128)>>8;
  638.   x4 = (181*(x4-x5)+128)>>8;
  639.   
  640.   /* fourth stage */
  641.   blk[0] = (x7+x1)>>8;
  642.   blk[1] = (x3+x2)>>8;
  643.   blk[2] = (x0+x4)>>8;
  644.   blk[3] = (x8+x6)>>8;
  645.   blk[4] = (x8-x6)>>8;
  646.   blk[5] = (x0-x4)>>8;
  647.   blk[6] = (x3-x2)>>8;
  648.   blk[7] = (x7-x1)>>8;
  649. }
  650.  
  651. /* column (vertical) IDCT
  652.  *
  653.  *             7                         pi         1
  654.  * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
  655.  *            l=0                        8          2
  656.  *
  657.  * where: c[0]    = 1/1024
  658.  *        c[1..7] = (1/1024)*sqrt(2)
  659.  */
  660. static void idctcol(blk)
  661. short *blk;
  662. {
  663.   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  664.  
  665.   /* shortcut */
  666.   if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
  667.         (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
  668.   {
  669.     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
  670.       iclp[(blk[8*0]+32)>>6];
  671.     return;
  672.   }
  673.  
  674.   x0 = (blk[8*0]<<8) + 8192;
  675.  
  676.   /* first stage */
  677.   x8 = W7*(x4+x5) + 4;
  678.   x4 = (x8+(W1-W7)*x4)>>3;
  679.   x5 = (x8-(W1+W7)*x5)>>3;
  680.   x8 = W3*(x6+x7) + 4;
  681.   x6 = (x8-(W3-W5)*x6)>>3;
  682.   x7 = (x8-(W3+W5)*x7)>>3;
  683.   
  684.   /* second stage */
  685.   x8 = x0 + x1;
  686.   x0 -= x1;
  687.   x1 = W6*(x3+x2) + 4;
  688.   x2 = (x1-(W2+W6)*x2)>>3;
  689.   x3 = (x1+(W2-W6)*x3)>>3;
  690.   x1 = x4 + x6;
  691.   x4 -= x6;
  692.   x6 = x5 + x7;
  693.   x5 -= x7;
  694.   
  695.   /* third stage */
  696.   x7 = x8 + x3;
  697.   x8 -= x3;
  698.   x3 = x0 + x2;
  699.   x0 -= x2;
  700.   x2 = (181*(x4+x5)+128)>>8;
  701.   x4 = (181*(x4-x5)+128)>>8;
  702.   
  703.   /* fourth stage */
  704.   blk[8*0] = iclp[(x7+x1)>>14];
  705.   blk[8*1] = iclp[(x3+x2)>>14];
  706.   blk[8*2] = iclp[(x0+x4)>>14];
  707.   blk[8*3] = iclp[(x8+x6)>>14];
  708.   blk[8*4] = iclp[(x8-x6)>>14];
  709.   blk[8*5] = iclp[(x0-x4)>>14];
  710.   blk[8*6] = iclp[(x3-x2)>>14];
  711.   blk[8*7] = iclp[(x7-x1)>>14];
  712. }
  713.  
  714. /* two dimensional inverse discrete cosine transform */
  715. void Fast_IDCT(block)
  716. short *block;
  717. {
  718.   int i;
  719.  
  720.   for (i=0; i<8; i++)
  721.     idctrow(block+8*i);
  722.  
  723.   for (i=0; i<8; i++)
  724.     idctcol(block+i);
  725. }
  726.  
  727. void Initialize_Fast_IDCT()
  728. {
  729.   int i;
  730.  
  731.   iclp = iclip+512;
  732.   for (i= -512; i<512; i++)
  733.     iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
  734. }
  735.